package com.drondea.testclient.handler.smgp;

import com.drondea.sms.channel.ChannelSession;
import com.drondea.sms.message.IMessage;
import com.drondea.sms.type.ICustomHandler;
import com.drondea.sms.type.UserChannelConfig;
import com.drondea.testclient.panel.SmgpPanel;
import com.drondea.testclient.service.provider.AbstractClientMessageProvider;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import lombok.extern.slf4j.Slf4j;


/**
 * @version V3.0.0
 * @description cmpp的定制处理器
 * @author 刘彦宁
 * @date 2020年06月23日17:35
 **/

@Slf4j
public class SmgpClientCustomHandler extends ICustomHandler {

    private static final SmgpPanel PANEL = SmgpPanel.getInstance();

    @Override
    public void fireUserLogin(Channel channel, ChannelSession channelSession) {
        // 设置发送按钮、断开按钮可用，连接按钮不可用
        PANEL.sendButton.setEnabled(true);
        PANEL.connectButton.setEnabled(false);
        // 连接数
        int sessionSize = channelSession.getSessionManager().getSessionSize();
        PANEL.connectedCountLabel.setText("当前连接: " + sessionSize);
        AbstractClientMessageProvider messageProvider = (AbstractClientMessageProvider) PANEL.sessionManager.getMessageProvider();
        messageProvider.init();
    }

    @Override
    public void channelClosed(ChannelSession channelSession) {
        //连接数
        int sessionSize = channelSession.getSessionManager().getSessionSize();
        PANEL.connectedCountLabel.setText("当前连接: " + sessionSize);
    }

    @Override
    public void configPipelineAfterLogin(ChannelPipeline pipeline) {
        pipeline.addLast("SmgpSubmitResponseHandler", new SmgpSubmitResponseHandler());
        pipeline.addLast("SmgpDeliveryRequestHandler", new SmgpDeliveryRequestHandler());
    }

    @Override
    public void responseMessageExpired(Integer sequenceId, IMessage request) {
        System.out.println("短信超时处理" + sequenceId);
    }

    @Override
    public void slidingWindowException(ChannelSession session, ChannelHandlerContext ctx, IMessage message, ChannelPromise promise, Exception exception) {
        log.error("slidingWindowException", exception);
    }

    @Override
    public boolean customLoginValidate(IMessage message, UserChannelConfig channelConfig, Channel channel) {
        return true;
    }

}
